package com.foreveross.fetl.infra.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.dayatang.utils.DateUtils;

public class DateUtil {

	/**
	   * 得到几天后的时间
	   * @param d
	   * @param day
	   * @return
	   */
	  public static String getDateAfter(int day){
	   Calendar now =Calendar.getInstance();
	   now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
	   String pattern = "yyyy-MM-dd";
	   SimpleDateFormat format = new SimpleDateFormat(pattern);
	   return format.format(now.getTime());
	  }

	  
	  /**
	   * 返回当天的日期Str格式
	   * @param datetime
	   * @return
	  */
	  
	  public static String getNowDateStr(String pattern){
		  Date todayTime=new Date();
		  if(pattern ==null ) return DateUtil.formatDay(todayTime);
		  else return DateUtil.formatDay(todayTime, pattern);
	  }
		/**
	   * 得到几天后的时间
	   * @param d
	   * @param day
	   * @return
	   */
	  public static Date addDay(Date date , int day){
		   Calendar now =Calendar.getInstance();
		   now.setTime(date);
		   now.set(Calendar.DATE,now.get(Calendar.DATE)+day);
		   return now.getTime();
	  }
	  /**
	   * 格式化日期
	   * @param date
	   * @return
	   */
	  public static String formatDay(Date date){
		  String pattern = "yyyy-MM-dd HH:mm:ss";
		  SimpleDateFormat format = new SimpleDateFormat(pattern);
		  return format.format(date);
	  }
	  
	  /**
	   * 格式化日期
	   * @param date
	   * @return
	   */
	  public static String formatDay(Date date, String pattern){
		  if(pattern == null || pattern.length() == 0)  pattern = "yyyy-MM-dd HH:mm:ss";
		  SimpleDateFormat format = new SimpleDateFormat(pattern);
		  return format.format(date);
	  }
	  /**
	   * 格式化时间
	   * @param formatTime
	   * @return
	   */
	  public static String formatTime(String formatTime)
	  {
		  String pattern = "HH:mm";
		  SimpleDateFormat fmt = new SimpleDateFormat(pattern);
		  return fmt.format(formatTime);
	  }
	  
	  

	/**
	 * 得到几天后的时间
	 * 
	 * @param d
	 * @param day
	 * @return
	 */
	public static String getDateAfter(Date date, int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		String pattern = "yyyy-MM-dd";
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(now.getTime());
	}


	public static Date addTime(Date date, int time) {
		Calendar now = Calendar.getInstance();
		now.setTime(date);
		now.add(Calendar.MILLISECOND, time);
		return now.getTime();
	}

	public static Date addTime(int time) {
		return addTime(new Date(), time);
	}


	/**
	 * 将字符串按照格式转成date类型
	 * 
	 * @param strFormat
	 * @param dateValue
	 * @return
	 */
	public static Date StringToDate(String strFormat, String dateValue) {
		if (dateValue == null)
			return null;

		if (strFormat == null)
			strFormat = "yyyy-MM-dd";

		SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
		Date newDate = null;

		try {
			newDate = dateFormat.parse(dateValue);
		} catch (ParseException pe) {
			pe.printStackTrace();
			newDate = null;
		}

		return newDate;
	}
	
	public static Date StringToDate(String strFormat, long dateValue) {
		if (dateValue<0 || dateValue==0)
			return null;

		if (strFormat == null)
			strFormat = "yyyy-MM-dd";

		SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
		Date newDate = null;

		try {
			newDate = dateFormat.parse(DateToString("yyyy-MM-dd HH:mm:ss",dateValue));
		} catch (ParseException pe) {
			pe.printStackTrace();
			newDate = null;
		}

		return newDate;
	}
	
	public static String DateToString(String strFormat, long dateValue) {
		if (dateValue<0 || dateValue==0)
			return null;

		if (strFormat == null)
			strFormat = "yyyy-MM-dd";

		SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
		String newDate = null;
		newDate = dateFormat.format(new Date(dateValue*1000));
		return newDate;
	}

	public static Date getLast3Month() {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, 3);
		Date otherDate = cal.getTime();
		// System.out.println( "today:   "+dateFormat.format(date)+
		// "   3   months   after:   "+dateFormat.format(otherDate));
		return otherDate;
	}

	/**
	 * 输入日期格式，是否早于时间
	 * 
	 * @param dateStr
	 * @return
	 */
	public static int compare_date(String DATE1, String DATE2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 两个日期相加 10：10 + 3h20m=13:30
	 * 
	 * @param Date
	 * @return
	 */
	public Date theEndDateTimes(String beginTime, String addTimes)
			throws Exception {
		String[] addTime = addTimes.replace("m", "").split("h");
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Calendar c = Calendar.getInstance();
		Date beginDateTimes = df.parse(beginTime);
		c.setTime(beginDateTimes);
		c.add(Calendar.HOUR, Integer.parseInt(addTime[0]));
		c.add(Calendar.MINUTE, Integer.parseInt(addTime[1]));
		Date endDateTimes = c.getTime();
		return endDateTimes;
	}

	/**
	 * 两个日期比较
	 * 
	 * @param boolean
	 * @return
	 */
	public static Boolean compareDate(String dateStr1, String dateStr2)
			throws Exception {
		boolean falg = false;
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date dt1 = df.parse(dateStr1);
		Date dt2 = df.parse(dateStr2);
		if (dt1.after(dt2)) {
			falg = true;
		}
		return falg;
	}

	/**
	 * 拿到当天凌晨的时间date
	 * 
	 * @return
	 */
	public static Date getEarlyMorning() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static void main(String[] args) {
		System.out.println(DateUtil.formatDay(DateUtils.getPrevDay(new Date()), "yyyyMMdd"));
	}
}
